package com.atlyn.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletConfig().getServletContext();
        System.out.println("context-param参数username的值是；" + context.getInitParameter("username"));
        System.out.println("context-param参数password的值是；" + context.getInitParameter("password"));
        //获取当前的工程路径，格式：/工程
        System.out.println("当前工程路径：" + context.getContextPath());
        //获取工程部署后在服务器硬盘上的绝对路径
        /*
        * /斜杠被服务器解析为http://ip:port/工程名/ 映射到IDEA代码的web目录<br/>
        * */
        System.out.println("工程部署的路径是：" + context.getRealPath("/"));
        System.out.println("工程下css文件的绝对路径是：" +context.getRealPath("/实验二.css"));
        System.out.println("工程下img目录的img1.png的绝对路径是：" + context.getRealPath("/img/img1.png"));
    }
}
